package com.david.viewtest.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Shader;
import android.util.AttributeSet;

public class GradientTextView extends androidx.appcompat.widget.AppCompatTextView {

    private LinearGradient linearGradient;

    public GradientTextView(Context context) {
        super(context);
    }

    public GradientTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public GradientTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    int startColor = Color.RED;
    int endColor = Color.BLUE;

    int shadowColor = Color.GRAY;

    public void setColor(int startColor, int endColor, int shadowColor) {
        this.startColor = startColor;
        this.endColor = endColor;
        this.shadowColor = shadowColor;
        invalidate();
    }

    Paint paint = new Paint();
    @Override
    protected void onDraw(Canvas canvas) {
        paint.setTextSize(90f);
        if (linearGradient == null) {
            int width = getWidth();
            int height = getHeight();
            linearGradient = new LinearGradient(0, 0, 0, height,
                    new int[]{startColor, startColor, endColor, endColor},
                    new float[]{0f, 0.3f, 0.7f, 1f}, Shader.TileMode.CLAMP);
        }


        paint.setShader(linearGradient);
//        canvas.drawRect(0, 0, getWidth(), getHeight(), paint);
        getPaint().setShader(linearGradient);
//        canvas.drawText(getText().toString(), 10, 70, paint);
//        setShadowLayer(4f, 4f, 4f, shadowColor);
        super.onDraw(canvas);
    }


    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        linearGradient = null;
    }
}

